<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
#!/usr/bin/env groovy

node {
    stage('checkout') {
        checkout scm
    }

<%_ if (sendBuildToGitlab) { _%>
    gitlabCommitStatus('build') {
<%_ } _%>
<%_ if (insideDocker) { _%>
<%= gitLabIndent %>    docker.image('jhipster/jhipster:v<%= jhipsterVersion %>').inside('-u jhipster<% if (buildToolMaven) { %> -e MAVEN_OPTS="-Duser.home=./"<% } else if (buildToolGradle) { %> -e GRADLE_USER_HOME=.gradle<% } %>') {
<%_ } _%>
<%= indent %>    stage('check java') {
<%= indent %>        sh "java -version"
<%= indent %>    }

<%_ if (buildToolMaven) { _%>
<%= indent %>    stage('clean') {
<%= indent %>        sh "chmod +x mvnw"
<%= indent %>        sh "./mvnw -ntp clean -P-webapp"
<%= indent %>    }
<%= indent %>    stage('nohttp') {
<%= indent %>        sh "./mvnw -ntp checkstyle:check"
<%= indent %>    }

    <%_ if (!skipClient) { _%>
<%= indent %>    stage('install tools') {
<%= indent %>        sh "./mvnw -ntp com.github.eirslett:frontend-maven-plugin:install-node-and-npm@install-node-and-npm"
<%= indent %>    }

<%= indent %>    stage('npm install') {
<%= indent %>        sh "./mvnw -ntp com.github.eirslett:frontend-maven-plugin:npm"
<%= indent %>    }
    <%_ } _%>
    <%_ if (ciCdIntegrationsSnyk) { _%>
<%= indent %>    stage('Install Snyk CLI') {
<%= indent %>       sh """
<%= indent %>           curl -Lo ./snyk $(curl -s https://api.github.com/repos/snyk/snyk/releases/latest | grep "browser_download_url.*snyk-linux" | cut -d ':' -f 2,3 | tr -d \" | tr -d ' ')
<%= indent %>           chmod +x snyk
<%= indent %>       """
<%= indent %>    }
<%= indent %>    stage('Snyk test') {
<%= indent %>       sh './snyk test --all-projects'
<%= indent %>    }
<%= indent %>    stage('Snyk monitor') {
<%= indent %>       sh './snyk monitor --all-projects'
<%= indent %>    }
    <%_ } _%>
<%= indent %>    stage('backend tests') {
<%= indent %>        try {
<%= indent %>            sh "./mvnw -ntp verify -P-webapp"
<%= indent %>        } catch(err) {
<%= indent %>            throw err
<%= indent %>        } finally {
<%= indent %>            junit '**/target/surefire-reports/TEST-*.xml,**/target/failsafe-reports/TEST-*.xml'
<%= indent %>        }
<%= indent %>    }

    <%_ if (!skipClient) { _%>
<%= indent %>    stage('frontend tests') {
<%= indent %>        try {
        <%_ if (insideDocker) { _%>
<%= indent %>           sh "npm install"
<%= indent %>           sh "npm test"
        <%_ } else { _%>
<%= indent %>            sh "./mvnw -ntp com.github.eirslett:frontend-maven-plugin:<%- nodePackageManager %> -Dfrontend.<%- nodePackageManager %>.arguments='run test'"
        <%_ } _%>
<%= indent %>        } catch(err) {
<%= indent %>            throw err
<%= indent %>        } finally {
<%= indent %>            junit '**/target/test-results/TESTS-results-jest.xml'
<%= indent %>        }
<%= indent %>    }

    <%_ } _%>
    <%_ if (ciCdIntegrationsHeroku) { _%>
<%= indent %>    stage('package and deploy') {
<%= indent %>        sh "./mvnw -ntp com.heroku.sdk:heroku-maven-plugin:3.0.7:deploy -DskipTests -Pprod -Dheroku.buildpacks=heroku/jvm -Dheroku.appName=<%= herokuAppName %>"
    <%_ } else { _%>
<%= indent %>    stage('packaging') {
<%= indent %>        sh "./mvnw -ntp verify -P-webapp <% if (ciCdIntegrationsDeploy) { %>deploy <% } %>-Pprod -DskipTests"
    <%_ } _%>
<%= indent %>        archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true
<%= indent %>    }
<%_ } else if (buildToolGradle) { _%>
<%= indent %>    stage('clean') {
<%= indent %>        sh "chmod +x gradlew"
<%= indent %>        sh "./gradlew clean --no-daemon"
<%= indent %>    }
<%= indent %>    stage('nohttp') {
<%= indent %>        sh "./gradlew checkstyleNohttp --no-daemon"
<%= indent %>    }

    <%_ if (!skipClient) { _%>
<%= indent %>    stage('npm install') {
<%= indent %>        sh "./gradlew <%- nodePackageManager %>_install -PnodeInstall --no-daemon"
<%= indent %>    }
    <%_ } _%>
    <%_ if (ciCdIntegrationsSnyk) { _%>
<%= indent %>    stage('Install Snyk CLI') {
<%= indent %>       sh """
<%= indent %>           curl -Lo ./snyk $(curl -s https://api.github.com/repos/snyk/snyk/releases/latest | grep "browser_download_url.*snyk-linux" | cut -d ':' -f 2,3 | tr -d \" | tr -d ' ')
<%= indent %>           chmod +x snyk
<%= indent %>       """
<%= indent %>    }
<%= indent %>    stage('Snyk test') {
<%= indent %>       sh './snyk test --all-projects'
<%= indent %>    }
<%= indent %>    stage('Snyk monitor') {
<%= indent %>       sh './snyk monitor --all-projects'
<%= indent %>    }
    <%_ } _%>
<%= indent %>    stage('backend tests') {
<%= indent %>        try {
<%= indent %>            sh "./gradlew test integrationTest -PnodeInstall --no-daemon"
<%= indent %>        } catch(err) {
<%= indent %>            throw err
<%= indent %>        } finally {
<%= indent %>            junit '**/build/**/TEST-*.xml'
<%= indent %>        }
<%= indent %>    }

    <%_ if (!skipClient) { _%>
<%= indent %>    stage('frontend tests') {
<%= indent %>        try {
<%= indent %>            sh "./gradlew <%- nodePackageManager %>_run_test -PnodeInstall --no-daemon"
<%= indent %>        } catch(err) {
<%= indent %>            throw err
<%= indent %>        } finally {
<%= indent %>            junit '**/build/test-results/TESTS-*.xml'
<%= indent %>        }
<%= indent %>    }

    <%_ } _%>
<%= indent %>    stage('packaging') {
<%= indent %>        sh "./gradlew bootJar -x test -Pprod -PnodeInstall --no-daemon"
<%= indent %>        archiveArtifacts artifacts: '**/build/libs/*.jar', fingerprint: true
<%= indent %>    }

    <%_ if (ciCdIntegrationsHeroku) { _%>
<%= indent %>    stage('deployment') {
<%= indent %>        sh "./gradlew deployHeroku --no-daemon"
<%= indent %>    }

     <%_ } _%>
<%_ } _%>
<%_ if (ciCdIntegrationsSonar) { _%>
<%= indent %>    stage('quality analysis') {
<%= indent %>        withSonarQubeEnv('<%= sonarName %>') {
    <%_ if (buildToolMaven) { _%>
<%= indent %>            sh "./mvnw -ntp initialize sonar:sonar"
    <%_ } else if (buildToolGradle) { _%>
<%= indent %>            sh "./gradlew sonarqube --no-daemon"
    <%_ } _%>
<%= indent %>        }
<%= indent %>    }
<%_ } _%>
<%_ if (insideDocker) { _%>
<%= gitLabIndent %>    }
<%_ } _%>
<%_ if (ciCdIntegrationsPublishDocker) { _%>

<%= gitLabIndent %>    def dockerImage
<%= gitLabIndent %>    stage('publish docker') {
<%= gitLabIndent %>        // A pre-requisite to this step is to setup authentication to the docker registry
    <%_ if (buildToolMaven) { _%>
<%= gitLabIndent %>        // https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin#authentication-methods
<%= gitLabIndent %>        sh "./mvnw -ntp -Pprod verify jib:build"
    <%_ } else if (buildToolGradle) { _%>
<%= gitLabIndent %>        // https://github.com/GoogleContainerTools/jib/tree/master/jib-gradle-plugin#authentication-methods
<%= gitLabIndent %>        sh "./gradlew bootJar jib -Pprod -PnodeInstall --no-daemon"
    <%_ } _%>
<%= gitLabIndent %>    }
<%_ } _%>
<%_ if (sendBuildToGitlab) { _%>
    }
<%_ } _%>
}
